﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace SoftwareMonkeys.LogClimber.Entities
{
    public class Entry
    {
        private Guid id = Guid.Empty;
        [XmlAttribute]
        public Guid ID
        {
            get { return id; }
            set { id = value; }
        }

        private Log log;
        [XmlIgnore]
        public Log Log
        {
            get { return log; }
            set { log = value; }
        }

        private int indent;
        [XmlAttribute]
        public int Indent
        {
            get { return indent; }
            set { indent = value; }
        }

        private Entry parent;
        [XmlIgnore]
        public Entry Parent
        {
            get { return parent; }
            set { parent = value; }
        }

        private Guid parentID;
        public Guid ParentID
        {
            get {
                if (parent != null)
                    return parent.ID;
                return parentID; }
            set {
                parentID = value;

                if (parent != null)
                {
                    if (parent.ID != parentID)
                        parent = null;
                }
            }
        }

        private Entry[] childEntries = new Entry[] {};
        [XmlElement("Entry")]
        public Entry[] ChildEntries
        {
            get { return childEntries; }
            set
            {
                childEntries = value;

                if (childEntries != null)
                {
                    SetLog(childEntries);
                    SetParent(childEntries);
                }
            }
        }

        private string data = String.Empty;
        [XmlAttribute]
        public string Data
        {
            get { return data; }
            set { data = value; }
        }

        private string component = String.Empty;
        [XmlAttribute]
        public string Component
        {
            get { return component; }
            set { component = value; }
        }

        private string method = String.Empty;
        [XmlAttribute]
        public string Method
        {
            get { return method; }
            set { method = value; }
        }

        public void AppendChild(Entry entry)
        {
            if (entry == null)
                throw new ArgumentNullException("entry");

            SetLog(entry);
            SetParent(entry);

            List<Entry> list = new List<Entry>(childEntries);
            list.Add(entry);

            childEntries = list.ToArray();
        }

        public void SetLog(params Entry[] entries)
        {
            foreach (Entry entry in entries)
            {
                entry.Log = this.Log;

                if (entry.ChildEntries != null)
                    SetLog(entry.ChildEntries);
            }
        }

        public void SetParent(params Entry[] entries)
        {
            foreach (Entry entry in entries)
            {
                entry.Parent = this;
            }
        }
    }
}
